//
//  Validator.java
//  TopoManipApplet
//
//  Created by Thibaud PIQUET on 08/04/10.
//  Copyright 2010 __MyCompanyName__. All rights reserved.
//

package src.convertisseur;
import java.io.*;
import javax.xml.transform.Source;
import javax.xml.transform.stream.StreamSource;
import javax.xml.validation.*;
import org.xml.sax.SAXException;
import java.net.*;

/**
 *
 * @author Thibaud PIQUET
 */
public class ValidatorXML {
	
    File xmlToValidate;
	
    public ValidatorXML() { }
    
	public ValidatorXML(File xml) {
        this.xmlToValidate = xml;
    }
	
	/* validation of Generic File with xsd in .jar */
	public String validation() throws SAXException, IOException {
							
		SchemaFactory factory = SchemaFactory.newInstance("http://www.w3.org/2001/XMLSchema");

		Source in = new StreamSource(getClass().getResourceAsStream("/resources/TopologyXML.xsd"));
		
		Schema schema = factory.newSchema(in);

		Validator validator = schema.newValidator();

		Source source = new StreamSource(xmlToValidate.getAbsolutePath());

		try {
			validator.validate(source);
			return(xmlToValidate.getAbsolutePath() + " is valid.");
		}
		catch (SAXException ex) {
			return(xmlToValidate.getAbsolutePath() + " is not valid because \n"+ ex.getMessage());
		}
	}

	/* validation on the Internet for Totem Files */
	public String validationURL() throws SAXException, IOException {

		SchemaFactory factory = SchemaFactory.newInstance("http://www.w3.org/2001/XMLSchema");

		Schema schema = factory.newSchema(new URL("http://totem.run.montefiore.ulg.ac.be/Schema/Domain-v1_3.xsd"));

		Validator validator = schema.newValidator();

		Source source = new StreamSource(xmlToValidate.getAbsolutePath());

		try {
			validator.validate(source);
			return(xmlToValidate.getAbsolutePath() + " is valid.");
		}
		catch (SAXException ex) {
			return(xmlToValidate.getAbsolutePath() + " is not valid because \n"+ ex.getMessage());
		}
	}
	
	/* validation of TOTEM file with xsd in .jar */
	public String validationTOTEM() throws SAXException, IOException {
		
		SchemaFactory factory = SchemaFactory.newInstance("http://www.w3.org/2001/XMLSchema");
		
		Source in = new StreamSource(getClass().getResourceAsStream("/resources/TopologyTOTEM.xsd"));
		
		Schema schema = factory.newSchema(in);
		
		Validator validator = schema.newValidator();

		Source source = new StreamSource(xmlToValidate.getAbsolutePath());
		
		try {
			validator.validate(source);
			return(xmlToValidate.getAbsolutePath() + " is valid.");
		}
		catch (SAXException ex) {
			return(xmlToValidate.getAbsolutePath() + " is not valid because \n"+ ex.getMessage());
		}
	}
	
	/* validation of Configuration file with xsd in .jar */
	public String validationConfig() throws SAXException, IOException {
		
		SchemaFactory factory = SchemaFactory.newInstance("http://www.w3.org/2001/XMLSchema");
		
		Source in = new StreamSource(getClass().getResourceAsStream("/resources/Config.xsd"));
		Source toTest = new StreamSource(getClass().getResourceAsStream("/resources/configuration.xml"));

		Schema schema = factory.newSchema(in);
		
		Validator validator = schema.newValidator();
		
		Source source = toTest;
		
		try {
			validator.validate(source);
			return("Default values file is valid.");
		}
		catch (SAXException ex) {
			return("Default values file is not valid because \n"+ ex.getMessage());
		}
	}
}
